<template>
	<view> <!-- @click="openAuth('WRITE_EXTERNAL_STORAGE')" -->
		<u-form-item :required="item.required" :label="$t(item.label)" :key="item.key">
			<view style="display: flex;flex-direction: column;">
				<u-upload v-if="imageLoading" :action="action" :file-list="list" :max-size="imageMaxSize"
					:max-count="max_count" :width="width" :height="height" :auto-upload="autoUpload"
					:show-upload-list="showUploadList" :del-bg-color="delBgColor" :del-color="delColor"
					:upload-text="uploadText" :limit-type="limitType" @on-success="onSuccess" @on-remove="onRemove" />
				<view class="my-notice" v-if="false">{{ $t(item.placeholder) }}</view>
			</view>
		</u-form-item>

<!-- 		<yk-authpup ref="authpup" type="top" @changeAuth="changeAuth" :permissionID="permissionID"></yk-authpup> -->

	</view>
</template>
<script>
	import {
		HTTP_REQUEST_URL,
		IMAGE_MAX_SIZE,
		IMAGE_MAX_COUNT
	} from '@/config/app';
	import ykAuthpup from "@/components/yk-authpup/yk-authpup";

	// #ifndef MP-WEIXIN
	import permision from "@/js_sdk/wa-permission/permission.js"
	// #endif

	export default {
		components: {
			ykAuthpup
		},
		props: {
			item: {
				type: Object,
				default: {},
				required: true
			}
		},
		data() {
			return {
				max_count: 99,
				imageLoading: false,
				action: HTTP_REQUEST_URL + '/common/upload',
				imageMaxSize: IMAGE_MAX_SIZE,
				imageMaxCount: IMAGE_MAX_COUNT,
				autoUpload: true,
				showUploadList: true,
				delBgColor: '#fa3534',
				delColor: '#ffffff',
				uploadText: this.$t('选择图片'),
				limitType: ['png', 'jpg', 'jpeg', 'webp', 'gif', 'image'],
				list: [],
				platform: '',
				permissionID: 'WRITE_EXTERNAL_STORAGE',
				permisesions: [{
					key: "WRITE_EXTERNAL_STORAGE",
					value: false
				}, {
					key: "CAMERA",
					value: false,
				}]
			};
		},
		computed: {
			width() {
				return this.item.width || 150
			},
			height() {
				return this.item.height || 150
			}
		},
		mounted() {
			if (this.item.rules) {
				if (this.item.rules.length) {
					this.max_count = this.item.rules.length
				}
			}

			//如果传递的是数组
			console.log("================================")
			if (this.item.value instanceof Array) {
				this.list = this.item.value
			} else {
				//如果父组件传值为字符串-按照UI组件渲染标准格式化传递参数
				if (typeof this.item.value === 'string') {
					this.list = [{
						url: this.item.value
					}]
				} else {
					this.list = this.item.value
				}
			}
			
			this.imageLoading = true

			const systemInfo = uni.getSystemInfoSync();
			const platform = systemInfo.platform;
			this.platform = platform
			if (platform != 'ios') {
				this.requestAndroidPermission('camera')
				this.requestAndroidPermission('photoLibrary')
			}

		},
		methods: {
			async requestAndroidPermission(id) {
				var that = this
				var result = await permision.requestAndroidPermission(id)
				console.log(id, result)
				// if(result!=0){
				// 	for(var i=0;i<that.permisesions.length;i++){
				// 		if(that.permisesions[i].key=='CAMERA'){
				// 			that.permisesions[i].value = true
				// 		}
				// 	}
				// }
			},
			async requestAndroidPermission2(id) {
				var that = this
				var result = await permision.requestAndroidPermission(id)
				console.log(id, result)
				// if(result!=0){
				// 	for(var i=0;i<that.permisesions.length;i++){
				// 		if(that.permisesions[i].key=='WRITE_EXTERNAL_STORAGE'){
				// 			that.permisesions[i].value = true
				// 		}
				// 	}
				// }
			},
			//打开自定义权限目的弹框
			openAuth(permissionID = '') {
				//this.openAuth('CAMERA')
				// App.vue里面用新的组件啦
				// if (this.platform != 'ios') {
				// 	const not = this.permisesions.find(item => item.value === false);
				// 	if (not) {
				// 		this.permissionID = not.key;
				// 		setTimeout(() => {
				// 			this.$refs['authpup'].open();
				// 		}, 500)
				// 	}
				// }
				//这个是对应的权限 ACCESS_FINE_LOCATION 位置权限 / WRITE_EXTERNAL_STORAGE 存储空间/照片权限 / CAMERA相机权限 / CALL_PHONE 拨打电话
			},
			//用户授权权限后的回调
			changeAuth() {
				const currentPermission = this.permisesions.find(item => item.key === this.permissionID);
				currentPermission.value = true
				//这里是权限通过后执行自己的代码逻辑
				console.log('权限已授权，可执行自己的代码逻辑了');
				this.openAuth()
			},
			districts(val) {
				return val.length > 6 ? val.slice(0, 6) + "..." : val

			},
			onSuccess(data, index, lists, name) { // 上传成功
				// console.log('success', lists)
				this.setFromData(lists)
			},
			onRemove(index, lists, name) {
				// console.log('remove', lists)
				this.setFromData(lists)
			},
			setFromData(lists) {
				let formData = [];
				if (lists.length > 0) {
					for (let i = 0; i < lists.length; i++) {
						if (lists[i].response) {
							let temp = {
								url: lists[i].response.data.url
							}
							formData.push(temp)
						} else if (lists[i].url) {
							let temp = {
								url: lists[i].url
							}
							formData.push(temp)
						}
					}
				} else {
					formData = []
				}
				// console.log('formData', formData)
				this.$emit('change', this.item.key, formData)
			}
		}
	}
</script>

<style lang="scss" scoped>
	.my-notice {
		color: #999999;
	}
</style>